// 'SuperMan' Mod
// JetBoom

// This simple addon lets players control their client-side ragdoll when they're dead, instead of just sitting there.
// Doesn't quite work with gamemodes that FORCE you to spawn but I do think sandbox doesn't force you.

local head_attachment = 10
local lleg_attachment = 14
local rleg_attachment = 12

local function SuperMan_CalcView(pos, ang, fov)
	local MySelf = LocalPlayer()
	if MySelf:IsValid() and MySelf:GetRagdollEntity() and not MySelf:Alive() then
		local ent = MySelf:GetRagdollEntity()
		local phys = ent:GetPhysicsObjectNum(head_attachment)
		local lleg_phys = ent:GetPhysicsObjectNum(lleg_attachment)
		local rleg_phys = ent:GetPhysicsObjectNum(rleg_attachment)
		if phys:IsValid() then
			local aimvec = MySelf:GetAimVector()
			if MySelf:KeyDown(IN_FORWARD) then
				phys:ApplyForceCenter(aimvec * 5000 + Vector(0,0,500))
				local hz = phys:GetVelocity()
				lleg_phys:SetVelocity(hz)
				rleg_phys:SetVelocity(hz)
			end

			if MySelf:KeyDown(IN_MOVELEFT) then
				phys:ApplyForceCenter(MySelf:GetAimVector():Angle():Right() * -500)
			elseif MySelf:KeyDown(IN_MOVERIGHT) then
				phys:ApplyForceCenter(MySelf:GetAimVector():Angle():Right() * 500)
			end

			local tr = util.TraceLine({start=phys:GetPos(), endpos = phys:GetPos() + MySelf:GetAimVector() * -128}).HitPos
			if tr.HitNormal then
				return {origin=tr.HitPos + tr.HitNormal * 4}
			else
				return {origin=tr.HitPos}  // angles=phys:GetAngles()}
			end
		end
	end
end
hook.Add("CalcView", "SuperMan_CalcView", SuperMan_CalcView)
